﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace LPY.LeHealth.WebSite.Common
{
    public class TokenHelper
    {
        /// <summary>
        /// 创建用户Token
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <returns></returns>
        public static string CreateToken(long uid)
        {
            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            //失效时间20分钟
            var now = Math.Round((DateTime.UtcNow.AddMinutes(20) - unixEpoch).TotalSeconds);
            var payload = new Dictionary<string, object>(){
                        { "exp", now },
                        { "id", uid }//将用户Id返回客户端
                    };
            var secretKey = ConfigurationManager.AppSettings["jwt_secret_key"];
            string token = JWT.JsonWebToken.Encode(payload, secretKey, JWT.JwtHashAlgorithm.HS256);
            return token;
        }
    }
}